package com.company.排序.冒泡排序;

import java.util.Arrays;

/**
 * @author uguoy089
 * @Description:
 * @Date: 2021/5/10 14:33
 *
 * 外层循环来控制所需找到最大值的次数，内层循环用来比较相邻的两个值之间的大小，若前面的值大于后面的值，则两个元素之间进行位置交换。
 * 每次外层循环所得到的都是最大元素的位置。
 */

public class Sort1 {

    public static void sort(int[] array) {
        int temp;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7,
                5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7,
                5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6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 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7,
                5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 75, 8, 6, 3, 9, 2, 1, 7, 5, 8, 6, 3, 9, 2, 1, 7};
        long startTime = System.currentTimeMillis();
//        System.out.println("startTime = " + startTime);
        sort(array);
        long endTime = System.currentTimeMillis();
//        System.out.println("endTime = " + endTime);
        System.out.println("time = " + (endTime-startTime));
        System.out.println(Arrays.toString(array));
    }
}
